/*
 *  CrossoverOperator.cpp
 *  Genetski
 *
 *  Created by Srđan Rašić on 11/19/11.
 *
 */

#include "CrossoverOperator.h"

OnePointCrossoverOperator::OnePointCrossoverOperator(int point) {
	_point = point;
}

std::pair<Chromosome, Chromosome> 
OnePointCrossoverOperator::Crossover(Chromosome& p1, Chromosome& p2) {
	Chromosome c1(p1);
	Chromosome c2(p2);
	
	for (int i = 0; i < p1.size(); i++) {
		if (i < _point)
			c2[i] = p1[i];
		else
			c1[i] = p2[i];
	}
	
	return make_pair(c1, c2);
}


std::pair<Chromosome, Chromosome> 
UniformCrossoverOperator::Crossover(Chromosome& p1, Chromosome& p2) {
	Chromosome mask(1, p1.size());
	Chromosome c1(p1);
	Chromosome c2(p2);
	
	for (int i = 0; i < mask.size(); i++) {
		if (mask[i] == '1') {
			c1[i] = p1[i];
			c2[i] = p2[i];
		}
		else {
			c1[i] = p2[i];
			c2[i] = p1[i];
		}
	}
	
	return make_pair(c1, c2);
}